import 'package:flutter/material.dart';
import './item.dart';
import './page.dart';

const catalogLength = 200;

Future<ItemPage> fetchPage(int startingIndex) async {
  await Future<void>.delayed(const Duration(microseconds: 500));// 模拟网络延时

  if (startingIndex > catalogLength) {// 起始index值超过catalog的长度，catalog中已经没有需要加载的数据
    return ItemPage(items: [], startingIndex: startingIndex, hasNext: false);
  }

  // 创建并返回新的ItemPage
  return ItemPage(
    items: List.generate(// 调用List类的generate方法，生成新的item
      itemsPerPage,
      (index) => Item(
        color: Colors.primaries[index % Colors.primaries.length],
        price: 50 + (index * 42) % 200,
        name: 'Color #${startingIndex + index}',
      ),
    ),
    startingIndex: startingIndex,
    hasNext: startingIndex + itemsPerPage < catalogLength,
  );
}
